package com.sunday.aomicaichang.ui.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunday.common.base.BaseFragment;
import com.sunday.common.model.ResultDO;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.common.widgets.ptr.PtrClassicFrameLayout;
import com.sunday.common.widgets.ptr.PtrDefaultHandler;
import com.sunday.common.widgets.ptr.PtrFrameLayout;
import com.sunday.common.widgets.ptr.PtrHandler;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.adpter.FindAdapter;
import com.sunday.aomicaichang.base.BaseApp;
import com.sunday.aomicaichang.http.ApiClient;
import com.sunday.aomicaichang.model.Find;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/6/4.
 */

public class FindFragmentChild extends BaseFragment {


    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.ptr_frame)
    PtrClassicFrameLayout ptrFrame;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private int type;
    private String statuses;
    private FindAdapter adapter;
    private List<Find> dataSet = new ArrayList<>();
    private long memberId;

    public static FindFragmentChild newInstance(String statuses) {
        FindFragmentChild fragment = new FindFragmentChild();
        Bundle bundle = new Bundle();
        bundle.putString("statuses", statuses);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_order_list, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            statuses = getArguments().getString("statuses");
        }
        Log.i("1111",statuses+"");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initRecyclerView();
        getData();
    }

    private LinearLayoutManager layoutManager;

    private void initRecyclerView() {
        emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext).drawable(R.drawable.horizontal_space_divider).build());
        adapter = new FindAdapter(mContext);
        adapter.setmData(dataSet);
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        ptrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getData();
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, recyclerView, header);
            }
        });
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });

    }

    private void getData() {
        showLoadingDialog(0);
        memberId = BaseApp.getInstance().getMember().getId();
        Call<ResultDO<List<Find>>> call = ApiClient.getApiAdapter().getAdvertisementByZoneKey(statuses);
        call.enqueue(new Callback<ResultDO<List<Find>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Find>>> call, Response<ResultDO<List<Find>>> response) {
                ptrFrame.refreshComplete();
                dissMissDialog();
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    if (response.body().getResult() == null) {
                        return;
                    }
                    if (dataSet != null)
                        dataSet.clear();
                    List<Find> finds=response.body().getResult();
                    if(finds.size()>0)
                        for(int i=0;i<finds.size();i++) {
                            if (("fanxian_roll").equals(finds.get(i).getZoneKey())) {
                                Find find=new Find();
                                find.setType(1);
                                find.setAdvertisements(finds.get(i).getAdvertisements());
                                dataSet.add(find);
                            }else {
                                Find find=new Find();
                                find.setType(2);
                                find.setAdvertisements(finds.get(i).getAdvertisements());
                                dataSet.add(find);
                            }
                        }
                    if (dataSet.size() > 0) {
                        emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
                    } else {
                        emptyLayout.setErrorType(EmptyLayout.NODATA);
                    }
                    adapter.setmData(dataSet);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<Find>>> call, Throwable t) {
                ptrFrame.refreshComplete();
            }
        });

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}

